package com.xuchen.zhenyang.controller.statistics;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.User;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoods;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoodsMonth;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoodsYear;
import com.xuchen.zhenyang.entity.statistics.StatisticsUserOrder;
import com.xuchen.zhenyang.entity.statistics.StatisticsUserOrderMonth;
import com.xuchen.zhenyang.entity.statistics.StatisticsUserOrderYear;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.model.base.StatisticsBaseQuery;
import com.xuchen.zhenyang.model.statistics.StatisticsGoodsQuery;
import com.xuchen.zhenyang.model.statistics.StatisticsGoodsSaleModel;
import com.xuchen.zhenyang.model.statistics.StatisticsUserOrderModel;
import com.xuchen.zhenyang.service.UserService;
import com.xuchen.zhenyang.service.statistics.StatisticsUserOrderMonthService;
import com.xuchen.zhenyang.service.statistics.StatisticsUserOrderService;
import com.xuchen.zhenyang.service.statistics.StatisticsUserOrderYearService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户总消费 前端控制器
 * </p>
 *
 * @author xuchen
 * @since 2019-05-11
 */
@Controller
@RequestMapping("/statisticsUserOrder")
@RequiresPermissions("statisticsUserOrder")
public class StatisticsUserOrderController {
    @Autowired
    StatisticsUserOrderMonthService userOrderMonthService;
    @Autowired
    StatisticsUserOrderYearService userOrderYearService;
    @Autowired
    StatisticsUserOrderService userOrderService;
    @Autowired
    UserService userService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "statistics/user/order/statistics-user-order-list";
    }

    @GetMapping("list")
    @ResponseBody
    @RequestLog
    Result list(BaseQuery baseQuery, StatisticsBaseQuery statisticsBaseQuery) {
        List<StatisticsUserOrderModel> list = new ArrayList<>(baseQuery.getLimit());
        if (statisticsBaseQuery == null) {
            statisticsBaseQuery = new StatisticsBaseQuery();
        }
        if (statisticsBaseQuery.getTimeType() == null) {
            //默认查询月
            statisticsBaseQuery.setTimeType(1);
        }
        switch (statisticsBaseQuery.getTimeType()) {
            case 1:
                StatisticsUserOrderMonth userOrderMonth = new StatisticsUserOrderMonth();
                userOrderMonth.setUserId(statisticsBaseQuery.getUserId());
                MyEntityWrapper<StatisticsUserOrderMonth> wrapper = new MyEntityWrapper<>(baseQuery, userOrderMonth);
                wrapper.eq("user_id").le("month_time", baseQuery.getMonthEndDate()).ge("month_time", baseQuery.getMonthBeginDate());
                userOrderMonthService.selectList(wrapper).forEach(e -> {
                    StatisticsUserOrderModel orderModel = new StatisticsUserOrderModel();
                    BeanUtils.copyProperties(e, orderModel);
                    orderModel.setUserOrderTime(String.format("%d年%d月", e.getMonthTime() / 100, e.getMonthTime() % 100));
                    list.add(orderModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            case 2:
                StatisticsUserOrderYear orderYear = new StatisticsUserOrderYear();
                orderYear.setUserId(statisticsBaseQuery.getUserId());
                MyEntityWrapper<StatisticsUserOrderYear> yearWrapper = new MyEntityWrapper<>(baseQuery, orderYear);
                yearWrapper.eq("user_id").le("year_time", baseQuery.getYearEndDate()).ge("year_time", baseQuery.getYearBeginDate());
                userOrderYearService.selectList(yearWrapper).forEach(e -> {
                    StatisticsUserOrderModel orderModel = new StatisticsUserOrderModel();
                    BeanUtils.copyProperties(e, orderModel);
                    orderModel.setUserOrderTime(String.format("%d年", e.getYearTime()));
                    list.add(orderModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            case 3:
                StatisticsUserOrder order = new StatisticsUserOrder();
                order.setUserId(statisticsBaseQuery.getUserId());
                MyEntityWrapper<StatisticsUserOrder> orderWrapper = new MyEntityWrapper<>(baseQuery, order);
                orderWrapper.eq("user_id");
                userOrderService.selectList(orderWrapper).forEach(e -> {
                    StatisticsUserOrderModel orderModel = new StatisticsUserOrderModel();
                    BeanUtils.copyProperties(e, orderModel);
                    orderModel.setUserOrderTime("所有时间");
                    list.add(orderModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            default:
                return null;
        }
    }

    private void setAttributeEnums(HttpServletRequest request) {
        List<User> list = userService.selectList(new EntityWrapper<User>().orderBy("user_name"));
        request.setAttribute("userList", list);
    }
}

